package net.lahuobao.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Administrator on 2016/11/4.
 */
public class MathUtils {

    public static String round(final Double amount, final int bit) {
        return new BigDecimal(amount.toString()).setScale(bit, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String round6(final double amount) {
        DecimalFormat format = new DecimalFormat("###0.000000");
        format.setRoundingMode(RoundingMode.HALF_UP);
        return format.format(amount);
    }

    public static double strToDouble(final Object obj) {
        String reg = "^[-+]?[0-9]+(.[0-9]+)?$";
        if(obj == null || !obj.toString().matches(reg)) {
            return 0;
        }
        return Double.parseDouble(obj.toString());
    }
}
